package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.Customer;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 : 客户Dao
 * 作者 : zdl
 * 日期 : 2017/6/28 16:44
 */
@Repository
public class CustomerDao extends BaseDao {

    public void saveCustomer(Customer customer) {
        super.save(customer);
    }

    public void deleteCustomer(Customer customer) {
        super.delete(customer);
    }

    public void updateCustomer(Customer customer) {
        super.update(customer);
    }

    public void saveOrUpdateCustomer(Customer customer) {
        super.saveOrUpdate(customer);
    }

    public Customer getCustomerById(String id) {
        return (Customer) super.get(Customer.class, id);
    }

    public List<Customer> listAllCustomer() {
        String hql = "FROM Customer WHERE isDelete=0";
        return super.find(hql);
    }

    public Page<Customer> pageByCustomer(Page<Customer> page, Customer customer) {
        StringBuilder hqlSb = new StringBuilder();
        hqlSb.append("FROM Customer c WHERE c.isDelete = 0 ");
        if (customer != null) {
            Map<String, Object> param = new HashMap<>();
            String customerName = customer.getName();
            if (StringUtils.isNotBlank(customerName)) {
                hqlSb.append(" AND c.name LIKE ?1 ");
                param.put("1", "%" + customerName + "%");
            }
            String contact = customer.getContact();
            if (StringUtils.isNotBlank(contact)) {
                hqlSb.append(" AND c.contact LIKE ?2 ");
                param.put("2", "%" + contact + "%");
            }
            Integer level = customer.getLevel();
            if (level != null && level != -1) {
                hqlSb.append(" AND c.level = ?3 ");
                param.put("3", level);
            }
            String userIds = customer.getUserIds();
            if (StringUtils.isNotBlank(userIds)) {
                hqlSb.append(" AND c.chargeId IN (?4) ");
                param.put("4", userIds);
            }
            hqlSb.append(" ORDER BY c.createTime DESC ");
            return super.findByPage(page, hqlSb.toString(), param);
        }
        return page;
    }

    public Customer getCustomerByName(String name) {
        if (StringUtils.isNotBlank(name)) {
            String hql = "FROM Customer WHERE isDelete = 0 AND name = '" + name + "'";
            return (Customer) super.get(hql);
        }
        return null;
    }

    public Customer getCustomerByContact(String contact) {
        if (StringUtils.isNotBlank(contact)) {
            String hql = "FROM Customer WHERE isDelete = 0 AND contact = '" + contact + "'";
            return (Customer) super.get(hql);
        }
        return null;
    }

    /**
     * 根据客户关键字查询客户list
     *
     * @param keyword 关键字(客户姓名、客户地址、联系方式)
     * @return list
     */
    public List<Customer> listCustomerByKeyword(String keyword) {
        if (StringUtils.isNotBlank(keyword)) {
            String hql = "FROM Customer WHERE isDelete = 0 AND (name LIKE ?1 OR contact LIKE ?2 OR address LIKE ?3)";
            Map<String, Object> param = new HashMap<>();
            param.put("1", keyword);
            param.put("2", keyword);
            param.put("3", keyword);
            return super.find(hql, param);
        }
        return null;
    }

    /**
     * 根据企业代码查询企业下所有客户
     *
     * @param comCode 企业代码
     * @return list
     */
    public List<Customer> listCustomerByComCode(String comCode) {
        if (StringUtils.isNotBlank(comCode)) {
            String hql = "FROM Customer WHERE isDelete = 0 AND comCode = '" + comCode + "' ";
            return super.find(hql);
        }
        return null;
    }

}
